﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Calc
{
    public partial class frmSelectItems : Form
    {
        public frmSelectItems()
        {
            InitializeComponent();
        }
        int CheckedAssemblyID = -1;
        int CheckedGroupIDAss = -1;
        int CheckedItemID = -1;
        ManageItems objManageItems = new ManageItems();
        Entities objEntities = new Entities();
        ProjectDB objProjectDB = new ProjectDB();
        ProjectEntities objProjectEntities = new ProjectEntities();

        private void frmSelectItems_Load(object sender, EventArgs e)
        {
            GetAssemblies();
            GetGroups();
            GetItems();
            
            LoadMeasurements();
            

            //DataSet ds = new DataSet();
            //objEntities.Action = 0;
            //ds = objManageItems.GetGroups(objEntities);
            
        }
        public void LoadMeasurements()
        {
            
            if (ProjectDB.MeasurementID != 0)
            {
                DataSet ds = new DataSet();
                ds = objProjectDB.LoadMeasurementsByMeasurementID();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    lblTitle.Text = lblTitle.Text + " " + ds.Tables[0].Rows[0]["Description"].ToString();
                    lblOriginalMeasurement.Text = ds.Tables[0].Rows[0]["OriginalMeasurement"].ToString();
                    lblCorrectedMeasurement.Text = ds.Tables[0].Rows[0]["CorrectedMeasurement"].ToString();
                }
            }
            this.Refresh();
        }
        private void GetGroups()
        {
            CheckedGroupIDAss = -1;
            listSelectGroups.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.GroupID = 0;
            objEntities.GroupName = "";
            objEntities.Action = 0;
            ds = objManageItems.GetGroups(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["GroupName"].ToString() != "(none)")
                {
                    ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["GroupName"].ToString());
                    lv.SubItems.Add(ds.Tables[0].Rows[i]["GroupID"].ToString());
                    listSelectGroups.Items.Add(lv);
                }
            }
            listSelectGroups.Items.Insert(0, "All Groups");
        }
        private void GetAssemblies()
        {
            CheckedAssemblyID = -1;
            listSelectAssemblies.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.AssemblyID = 0;
            objEntities.AssemblyName = "";
            objEntities.Action = 0;
            ds = objManageItems.GetAssemblies(objEntities);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["AssemblyName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["AssemblyID"].ToString());
                listSelectAssemblies.Items.Add(lv);
            }
            //listGroups.Items.Insert(0, "All Groups");
            //listGroupsAss.Items.Insert(0, "All Groups");
        }

        private void listSelectAssemblies_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            GetItems();
            if (CheckedAssemblyID != -1)
            {
                listSelectAssemblies.Items[CheckedAssemblyID].Checked = false;
            }
            if (e.Item.Checked == true)
            {
                objEntities.AssemblyID = Convert.ToInt16(e.Item.SubItems[1].Text);
                DataSet ds = new DataSet();
                ds = objManageItems.GetItemsByAssemblies(objEntities);
                CheckedAssemblyID = e.Item.Index;
                for (int j = 0; j < listSelectItems.Items.Count; j++)
                {
                    if (listSelectItems.Items[j].Checked == true)
                        listSelectItems.Items[j].Checked = false;
                }
                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        for (int j = 0; j < listSelectItems.Items.Count; j++)
                        {
                            if (ds.Tables[0].Rows[i]["ItemID"].ToString() == listSelectItems.Items[j].SubItems[3].Text)
                            {
                                listSelectItems.Items[j].Checked = true;
                                //break;
                            }
                        }
                    }
                }
            }
        }

        private void listSelectGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < listSelectGroups.SelectedIndices.Count; i++)
            { 
                
            }
        }
        private void GetItems()
        {
            CheckedItemID = -1;
            listSelectItems.Items.Clear();
            DataSet ds = new DataSet();
            objEntities.Action = 0;
            objEntities.ItemID = 0;
            objEntities.GroupID = 0;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                listSelectItems.Items.Add(lv);
            }

        }
        private void listSelectGroups_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            if (CheckedGroupIDAss != -1)
            {
                listSelectGroups.Items[CheckedGroupIDAss].Checked = false;
                //listGroups.Items[CheckedGroupID].Checked = false;
            }
            if (e.Item.Checked == true)
            {

                if (e.Item.Text != "All Groups")
                {
                    CheckedGroupIDAss = Convert.ToInt16(e.Item.Index);
                    //listGroups.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(Convert.ToInt16(e.Item.SubItems[1].Text));
                }
                else
                {
                    CheckedGroupIDAss = 0;
                    //listGroups.Items[CheckedGroupID].Checked = true;
                    GetItemsByGroup(0);
                }
            }
        }
        private void GetItemsByGroup(int GroupID)
        {
            //CheckedItemID = -1;
            //CheckedGroupID=-1;
            listSelectItems.Items.Clear();
            //listItemsAss.Items.Clear();
            DataSet ds = new DataSet();
            if (GroupID != 0)
                objEntities.Action = 2;
            else
                objEntities.Action = 0;
            objEntities.ItemID = 0;
            objEntities.GroupID = GroupID;
            ds = objManageItems.GetItems(objEntities);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ListViewItem lv = new ListViewItem(ds.Tables[0].Rows[i]["GroupName"].ToString());
                ListViewItem lv1 = new ListViewItem(ds.Tables[0].Rows[i]["ItemName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                //lv1.SubItems.Add(ds.Tables[0].Rows[i]["MaterialName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemName"].ToString());
                //lv1.SubItems.Add(ds.Tables[0].Rows[i]["GroupName"].ToString());
                lv.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                //lv1.SubItems.Add(ds.Tables[0].Rows[i]["ItemID"].ToString());
                listSelectItems.Items.Add(lv);
                //listItemsAss.Items.Add(lv1);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

       

        private void btnCorrectMeasurement_Click(object sender, EventArgs e)
        {
            frmRoofCalculator objRoofCalculator = new frmRoofCalculator();
            objRoofCalculator.ShowDialog();
            this.Refresh();
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            DataSet dsMeasurement=new DataSet();
            dsMeasurement = objProjectDB.LoadMeasurementsByMeasurementID();
            
            if (dsMeasurement.Tables[0].Rows.Count>0)
            {
                objProjectEntities.Measurement=Convert.ToInt16(dsMeasurement.Tables[0].Rows[0]["MeasurementID"].ToString());
                objProjectEntities.MeasurementUnits=dsMeasurement.Tables[0].Rows[0]["MeasurementUnits"].ToString();
                if (dsMeasurement.Tables[0].Rows[0]["OriginalMeasurement"].ToString() != "")
                    objProjectEntities.OriginalMeasurement = Convert.ToInt16(dsMeasurement.Tables[0].Rows[0]["OriginalMeasurement"].ToString());
                else
                    objProjectEntities.OriginalMeasurement = 0;
                if (dsMeasurement.Tables[0].Rows[0]["CorrectedMeasurement"].ToString() != "")
                    objProjectEntities.CorrectedMeasurement = Convert.ToInt16(dsMeasurement.Tables[0].Rows[0]["CorrectedMeasurement"].ToString());
                else
                    objProjectEntities.CorrectedMeasurement = 0;
            }

            for (int i = 0; i < listSelectItems.Items.Count; i++)
            {
                if (listSelectItems.Items[i].Checked == true)
                {
                    DataSet dsItems = new DataSet();
                    objEntities.Action = 1;
                    objEntities.ItemID = Convert.ToInt16(listSelectItems.Items[i].SubItems[3].Text);
                    objEntities.GroupID = 0;
                    dsItems = objManageItems.GetItems(objEntities);
                    if (dsItems.Tables[0].Rows.Count>0)
                    {
                        objEntities.ItemID=Convert.ToInt16(dsItems.Tables[0].Rows[0]["ItemID"].ToString());
                        objEntities.ItemName=dsItems.Tables[0].Rows[0]["ItemName"].ToString();
                        objEntities.GroupID=Convert.ToInt16(dsItems.Tables[0].Rows[0]["GroupID"].ToString());
                        objEntities.MaterialTypeID=Convert.ToInt16(dsItems.Tables[0].Rows[0]["MaterialTypeID"].ToString());
                        if (dsItems.Tables[0].Rows[0]["Cost"].ToString() != "")
                            objEntities.Cost = Convert.ToDecimal(dsItems.Tables[0].Rows[0]["Cost"].ToString());
                        else
                            objEntities.Cost = 0;
                        objEntities.CostPer=dsItems.Tables[0].Rows[0]["CostPer"].ToString();
                        objEntities.Coverage=dsItems.Tables[0].Rows[0]["Coverage"].ToString();
                        objEntities.Waste=dsItems.Tables[0].Rows[0]["Waste"].ToString();
                        objEntities.Labour = dsItems.Tables[0].Rows[0]["Labour"].ToString();
                        if (dsItems.Tables[0].Rows[0]["LabourType"].ToString() != "")
                            objEntities.LabourType = Convert.ToInt16(dsItems.Tables[0].Rows[0]["LabourType"].ToString());
                        else
                            objEntities.LabourType = 0;
                        objEntities.Action = 0;
                        objProjectDB.ManageMeasurementChild(0,objProjectEntities.Measurement, objProjectEntities.OriginalMeasurement, objProjectEntities.CorrectedMeasurement, objProjectEntities.Notes, objProjectEntities.MeasurementUnits, 1, objEntities.ItemID, objEntities.ItemName, objEntities.GroupID, objEntities.MaterialTypeID, objEntities.Cost, objEntities.CostPer, objEntities.Coverage, objEntities.Waste, objEntities.Labour, objEntities.LabourType, objEntities.Action);
                    }

                    
                }
            }
            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        

    }
}
